package customer.zspt.cn.customer.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.utils.TvToEnjoy;

/**
 * Created by Administrator on 2018/7/20 0020.
 */

public class HistoryItemAdapter extends BaseAdapter {
    Context mContext;
    List<Map<String,String>> mapList;
    BitmapUtils utils;
    TvToEnjoy tvToEnjoy;
    public HistoryItemAdapter(Context context, List<Map<String,String>> maps){
        mContext = context;
        mapList = maps;
        utils = new BitmapUtils(context);
        tvToEnjoy = new TvToEnjoy(context);
    }
    @Override
    public int getCount() {
        return mapList.size();
    }
    @Override
    public Object getItem(int position) {
        return mapList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null)
        {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext,R.layout.historyitem,null);
            viewHolder.user_header_ig = convertView.findViewById(R.id.header_portrait);
            viewHolder.userName = convertView.findViewById(R.id.userName);
            viewHolder.msg_content = convertView.findViewById(R.id.msg_content);
            viewHolder.Time = convertView.findViewById(R.id.Time);
            viewHolder.UD_count = convertView.findViewById(R.id.UD_count);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        utils.display(viewHolder.user_header_ig,mapList.get(position).get("userHearderIg"));
        viewHolder.userName.setText(mapList.get(position).get("userName"));
        viewHolder.msg_content.setText(tvToEnjoy.solveData(mapList.get(position).get("content")));
        long timestamp = Long.parseLong(mapList.get(position).get("timestamp"));
        String beijingTime = getBeijingTime(timestamp);
        viewHolder.Time.setText(beijingTime);
        if(Integer.parseInt(mapList.get(position).get("UD_count"))>0)
        {
            viewHolder.UD_count.setVisibility(View.VISIBLE);
            if(Integer.parseInt(mapList.get(position).get("UD_count"))>99)
            {
                viewHolder.UD_count.setText("99+");
            }
            else{
                viewHolder.UD_count.setText(mapList.get(position).get("UD_count"));
            }
        }
        else{
            viewHolder.UD_count.setVisibility(View.GONE);
        }
        return convertView;
    }
    class ViewHolder{
        //头像
        ImageView user_header_ig;
        //用户名
        TextView userName;
        //消息
        TextView msg_content;
        //时间
        TextView Time;;
        //未读消息
        TextView UD_count;
    }

    public String getBeijingTime(long time){
        String Truetime = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = sdf.format(new Date(time));
        String[] Hssplit = format.split(" ");
        SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String Nowformat = sdf.format(new Date(time));
        String[] Nwsplit = Nowformat.split(" ");
        if (Hssplit[0].equals(Nwsplit[0])){
            Truetime+="今天 "+Hssplit[1];
        }
        else if(Integer.parseInt(Nwsplit[0])-Integer.parseInt(Hssplit[0])==1)
        {
            Truetime+="昨天 "+Hssplit[1];
        }
        else if(Integer.parseInt(Nwsplit[0])-Integer.parseInt(Hssplit[0])==2)
        {
            Truetime+="前天 "+Hssplit[1];
        }
        else{
            Truetime+=format;
        }
        return Truetime;
    }
}
